package fenae.controls;

import lumis.doui.control.DataBoundControl;
import lumis.doui.source.ISourceData;
import lumis.doui.source.TabularData;
import lumis.doui.source.TabularSource;
import lumis.doui.source.field.ISourceField;
import lumis.doui.source.field.ITabularSourceField;
import lumis.portal.PortalException;
import lumis.service.image.field.ImageDataType;

public class VotacaoImagemVideoJuriControl extends
		DataBoundControl<TabularSource<?>> {
	@SuppressWarnings("unchecked")
	public void loadFromRequest() throws PortalException {
		super.loadFromRequest();
		((TabularSource) getSource()).setLoad(true);
	}

	@SuppressWarnings("unchecked")
	public void setRenderData() throws PortalException {
		super.setRenderData();

		TabularSource source = (TabularSource) getSource();

		buildControls(source);
	}

	private void buildControls(TabularSource<?> source) throws PortalException {
		super.buildSubControls();

		TabularData tabularData = source.getData();

		StringBuilder subControls = new StringBuilder();

		subControls.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");

		subControls.append("<controls>");

		if (tabularData.getRows().size() > 0) {
			subControls.append("<control type=\"lum_table\">");

			for (int rowIndex = 0; rowIndex < tabularData.getRows().size(); rowIndex++) {
				ISourceData row = (ISourceData) tabularData.getRows().get(
						rowIndex);

				if (rowIndex % 2 == 0) {
					subControls.append("<tr>");
				}

				subControls.append("<td valign='bottom'>");

				subControls
						.append("<control type=\"lum_table\" valign='bottom'>");

				String primaryKey = "";
				ISourceField primaryKeySourceField = (ISourceField) source
						.getPrimaryKeyField();
				if (primaryKeySourceField != null) {
					Object value = row.get(primaryKeySourceField.getId());
					if (value != null) {
						primaryKey = (String) primaryKeySourceField
								.getConverter().convert(String.class, value);
					}

				}

				for (ITabularSourceField field : source.getFields()) {
					String datatype = field.getDataType().getId();

					String fieldId = field.getId();
					Object value = row.get(fieldId);

					if ((fieldId.equals("categoriauid")) && (value != null)) {
						subControls.append("<tr>");
						subControls.append("<td>");
						subControls
								.append("<control type=\"lum_text\" value=\""
										+ value + "\"/>");
						subControls.append("</td>");
						subControls.append("</tr>");
					}

					if ((!datatype.equals("string"))
							&& (!datatype.equals("image"))
							&& (!datatype.equals("integer"))
							&& (!datatype.equals("guid")))
						continue;
					if (value == null)
						continue;
					subControls.append("<tr>");
					subControls.append("<td>");

					if (datatype.equals("image")) {
						ImageDataType.Data imagem = (ImageDataType.Data) value;

						subControls
								.append("<control type=\"lum_inputHidden\" value=\""
										+ imagem.getFileId()
										+ "\" id=\""
										+ fieldId + "_" + primaryKey + "\"/>");
					} else if (datatype.equals("integer")) {
						Integer medida = (Integer) value;

						subControls
								.append("<control type=\"lum_inputHidden\" value=\""
										+ medida.toString()
										+ "\" id=\""
										+ fieldId + "_" + primaryKey + "\"/>");
					} else if (datatype.equals("guid")) {
						subControls
								.append("<control type=\"lum_inputHidden\" value=\""
										+ value
										+ "\" id=\""
										+ fieldId
										+ "_"
										+ primaryKey + "\"/>");
					} else if (datatype.equals("string")) {
						subControls
								.append("<control type=\"lum_text\" value=\""
										+ value + "\" id=\"texto_" + primaryKey
										+ "\"/>");
					}
					subControls.append("</td>");
					subControls.append("</tr>");
				}

				subControls.append("<tr>");
				subControls.append("<td>");
				subControls
						.append("<control type=\"lum_button\" text=\"Votar\">");
				subControls.append("<onClick type=\"script\">");
				subControls
						.append("<script>document.getElementById('trabalhouid').value = '"
								+ primaryKey + "';</script>");
				subControls.append("</onClick>");
				subControls.append("<onClick type=\"processAction\">");
				subControls
						.append("<processActionId>votacaoTexto</processActionId>");
				subControls.append("<addParameters>");
				subControls.append("<parameter name=\"trabalhouid\">");
				subControls.append("<value controlId=\"trabalhouid\"/>");
				subControls.append("</parameter>");
				subControls.append("</addParameters>");
				subControls.append("</onClick>");
				subControls.append("</control>");
				subControls.append("</td>");
				subControls.append("</tr>");
				subControls.append("</control>");
				subControls.append("</td>");

				if ((rowIndex % 2 != 1)
						&& (rowIndex != tabularData.getRows().size() - 1))
					continue;

				subControls.append("</tr>");
			}

			subControls.append("</control>");
		}
		subControls.append("</controls>");

		appendSubControls(subControls.toString());
	}
}
